﻿using DocumentFormat.OpenXml.Drawing.Charts;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Utils
{
    public class BitmapLuminanceSource : BaseLuminanceSource
    {
        public BitmapLuminanceSource(Bitmap bitmap) : base(bitmap.Width, bitmap.Height)
        {
            var lockData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            var stride = Math.Abs(lockData.Stride);
            var pixelCount = stride * Height;
            var pixels = new byte[pixelCount];
            System.Runtime.InteropServices.Marshal.Copy(lockData.Scan0, pixels, 0, pixelCount);
            bitmap.UnlockBits(lockData);

            for (int y = 0; y < Height; y++)
            {
                var offset = y * stride;
                for (int x = 0; x < Width; x++)
                {
                    var idx = offset + x * 4;
                    var luminance = (byte)(pixels[idx + 2] * 0.3 + pixels[idx + 1] * 0.59 + pixels[idx] * 0.11);
                    luminances[y * Width + x] = luminance;
                }
            }
        }

        protected override LuminanceSource CreateLuminanceSource(byte[] newLuminances, int width, int height)
        {
            throw new NotImplementedException();
        }
    }
}
